Import mojo
Import inputbutton

Class NumberInputButton extends InputButton
	Private
		Field number:Int
		Field nextNumber:Int
		Field buttonNdx:Int = 0
		Field tween2:Tween2
		Field currentTween:Int = TWEEN_FADE
		Field textOffsetX:Int
		Field textOffsetY:Int
		Field originalX:Int
		Field originalY:Int
		
	Public
		Const TWEEN_FADE:Int = 0
		Const TWEEN_MOVE:Int = 1

	
	Method setState:Void(state:Int)
		Select state
			Case TWEEN_FADE
				' Tween the alpha
				tween2.SetEquation(Tween2.Cubic.EaseOut)
				tween2.SetDuration(300)' needs to be the same
				tween2.SetValue(1,0.1)			
				tween2.Start()
				currentTween=TWEEN_FADE
			Case TWEEN_MOVE
				tween2.SetEquation(Tween2.Elastic.EaseOut)
				tween2.SetDuration(1500+(buttonNdx*100))
				tween2.SetValue(SCREEN_WIDTH+100,originalX)			
				tween2.Start()
				currentTween=TWEEN_MOVE
				
				' Don't want to see next number until finished fading out
				number = nextNumber
		End
	End
		
		
	Method setNumber:Void(number:int)
		Self.nextNumber = number
	
		textOffsetX=_model.numbersFont.GetTxtWidth(number)/2
		textOffsetY=_model.numbersFont.GetFontHeight()/2
	End
	
	Method GetNumber:int()
		Return number
	End
	
	Function Create:NumberInputButton(gi:GameImage, x#, y#, event:ButtonEvent, frame:Int=0,buttonNdx:Int, buttonsList:ArrayList<NumberInputButton> )
	
		Local button:NumberInputButton = new NumberInputButton()
		button.image = gi
		button.image.MidHandle(true)
		button.x = x
		button.y = y
		button.originalX = x
		button.originalY = y
	
		button.alpha = 1
		button.SetHitBox(-gi.w2, -gi.h2, gi.w, gi.h)
		button.event = event
		button.mainImage = frame
		button.buttonNdx = buttonNdx
		
		button.tween2 = New Tween2(Tween2.Elastic.EaseOut,0,0,0)
		
		buttonsList.Add(button)
		
		Return button
	End

	
	Function DrawAll:Void(buttonsList:ArrayList<NumberInputButton>)
		For Local ndx:Int=0 Until buttonsList.Size
			Local val:NumberInputButton = buttonsList.Get(ndx)
			val.Draw()
		End					
	End
	
	Function UpdateAll:Void(buttonsList:ArrayList<NumberInputButton>)
		For Local ndx:Int=0 Until buttonsList.Size
			Local val:NumberInputButton = buttonsList.Get(ndx)
			val.Update()
		End	
	End	
	
	Method Update:Void()
		Super.Update()
		tween2.Update()
		if(currentTween=TWEEN_FADE)
			if(tween2.isActive=false)				
				setState(TWEEN_MOVE)
			else				
				alpha=tween2.Value()	
				scaleX=	tween2.Value()
				scaleY=	tween2.Value()
				'x = x+width/2
			Endif
		ElseIf (currentTween=TWEEN_MOVE)
			alpha=1	
			scaleX=	1
			scaleY=	1		
			x=tween2.Value()
		endif
	End
	
	Method Draw:Void()
		Super.Draw()
		SetAlpha Self.alpha
		PushMatrix		
		Translate (x,y)
		Scale Self.scaleX,Self.scaleY
		_model.numbersFont.DrawText(number, -textOffsetX,-textOffsetY )
		PopMatrix
	End
End